import pygame
import os
import time
from config import config

# print(dir(pygame))


def getAbsPath(p):
    return os.path.abspath(os.path.join(__file__, r"../../%s" % p))


def play(filename, seconds=1000, hasPrefix=False):
    filePath = getAbsPath("media/%s" % filename) if not hasPrefix else filename
    if not os.path.exists(filePath):
        print("not found:", filePath)
        return False

    pygame.mixer.init()
    try:
        music = pygame.mixer.music
        music.load(filePath)
        music.play()
        time.sleep(seconds - 1)
        music.stop()
    except Exception as e:
        print(e)
        return False


def playGood():
    return play("good.mp3", 2)


def playBad():
    return play("cough.mp3", 2)


def playTips():
    return play("warn.mp3", 2)


def playWin():
    return play("win.mp3", 4)


if __name__ == "__main__":
    # play('good.mp3')
    play(r"逆流而上的你.mp3")
    # playGood()
    # playTips()
